package at.ac.tuwien.lsds.testDataGenerator;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

import at.ac.tuwien.lsds.csv.WriterImpl;
import at.ac.tuwien.lsds.model.ApplicationImpl;
import at.ac.tuwien.lsds.model.ScenarioEnum;
import at.ac.tuwien.lsds.util.ApplicationOutputFile;
import at.ac.tuwien.lsds.util.Settings;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class InputDataGenerator {
	
	private static final Logger log = Logger.getLogger(InputDataGenerator.class);
	
	// PARAMETERS
	private Integer minTimestamp;
	private Integer maxTimestamp;
	private Integer minHdd;
	private Integer maxHdd;
	private Integer minRAM;
	private Integer maxRAM;
	private Integer minCPU;
	private Integer maxCPU;
	private Integer minDuration;
	private Integer maxDuration;
	
	public void createInputDataFile(ScenarioEnum scenario) throws FileNotFoundException, IOException {
		setAllParameters(scenario);
		ApplicationOutputFile outputFile = new ApplicationOutputFile(scenario);
		Integer timestamp = 0, hdd, ram, cpu, duration;
		for (int i = 0; i < Settings.getGeneratorNumberOfApps(); i++) {
			timestamp += this.getRandomInt(minTimestamp, maxTimestamp);
			hdd = this.getRandomInt(minHdd, maxHdd);
			ram = this.getRandomInt(minRAM, maxRAM);
			cpu = this.getRandomInt(minCPU, maxCPU);
			duration = this.getRandomInt(minDuration, maxDuration);
			outputFile.addRow(new ApplicationImpl(hdd, ram, cpu, timestamp, duration));
		}
		
		new WriterImpl().writeToCSV(outputFile);
		log.info("CREATED: "+ outputFile.getFilePath() + " (" + Settings.getGeneratorNumberOfApps() + " Apps)");
	}
	
	private Integer getRandomInt(Integer min, Integer max) {
		return new Random().nextInt(max - min + 1) + min;
	}
	
	private void setAllParameters(ScenarioEnum scenario) {
		// equal for all Scenarios
		setMinTimestamp(0);
		setMaxTimestamp(30);
		setMinDuration(1);
		setMaxDuration(5000);

		if(scenario.equals(ScenarioEnum.A)) {
			setMinHdd(20);
			setMaxHdd(100);
			setMinCPU(50);
			setMaxCPU(100);
			setMinRAM(30);
			setMaxRAM(50);
		} else if (scenario.equals(ScenarioEnum.B)) {
			setMinHdd(150);
			setMaxHdd(500);
			setMinCPU(100);
			setMaxCPU(400);
			setMinRAM(100);
			setMaxRAM(700);
		} else {
			setMinHdd(20);
			setMaxHdd(500);
			setMinCPU(50);
			setMaxCPU(400);
			setMinRAM(30);
			setMaxRAM(700);
		}
		
	}

	
	
	
	
	/** ------------- GETTER AND SETTER ------------- **/
	
	public Integer getMinTimestamp() {
		return minTimestamp;
	}

	public void setMinTimestamp(Integer minTimestamp) {
		this.minTimestamp = minTimestamp;
	}

	public Integer getMaxTimestamp() {
		return maxTimestamp;
	}

	public void setMaxTimestamp(Integer maxTimestamp) {
		this.maxTimestamp = maxTimestamp;
	}

	public Integer getMinHdd() {
		return minHdd;
	}

	public void setMinHdd(Integer minHdd) {
		this.minHdd = minHdd;
	}

	public Integer getMaxHdd() {
		return maxHdd;
	}

	public void setMaxHdd(Integer maxHdd) {
		this.maxHdd = maxHdd;
	}

	public Integer getMinRAM() {
		return minRAM;
	}

	public void setMinRAM(Integer minRAM) {
		this.minRAM = minRAM;
	}

	public Integer getMaxRAM() {
		return maxRAM;
	}

	public void setMaxRAM(Integer maxRAM) {
		this.maxRAM = maxRAM;
	}

	public Integer getMinCPU() {
		return minCPU;
	}

	public void setMinCPU(Integer minCPU) {
		this.minCPU = minCPU;
	}

	public Integer getMaxCPU() {
		return maxCPU;
	}

	public void setMaxCPU(Integer maxCPU) {
		this.maxCPU = maxCPU;
	}

	public Integer getMinDuration() {
		return minDuration;
	}

	public void setMinDuration(Integer minDuration) {
		this.minDuration = minDuration;
	}

	public Integer getMaxDuration() {
		return maxDuration;
	}

	public void setMaxDuration(Integer maxDuration) {
		this.maxDuration = maxDuration;
	}


	
}
